package com.dlc.shop.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dlc.shop.allinpay.service.AllinpayCompanyService;
import com.dlc.shop.bean.dto.allinpay.AllinpayShopBankCardDTO;
import com.dlc.shop.bean.enums.AuditStatus;
import com.dlc.shop.bean.model.ShopBankCard;
import com.dlc.shop.bean.model.ShopCompany;
import com.dlc.shop.bean.model.ShopDetail;
import com.dlc.shop.bean.vo.ShopCompanyVO;
import com.dlc.shop.common.allinpay.constant.CompanyInfoProcessStatus;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.BeanUtil;
import com.dlc.shop.service.ShopBankCardService;
import com.dlc.shop.service.ShopCompanyService;
import com.dlc.shop.service.ShopDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author lth
 * @Date 2021/8/11 15:04
 */
@RestController
@AllArgsConstructor
@RequestMapping("/platform/shopBankCard")
@Tag(name = "银行卡相关接口")
public class ShopBankCardController {

    private final ShopBankCardService shopBankCardService;
    private final ShopCompanyService shopCompanyService;
    private final ShopDetailService shopDetailService;
    private final AllinpayCompanyService allinpayCompanyService;

    @GetMapping("/listByShopId")
    @Operation(summary = "根据店铺id批量获取银行卡信息")
    @Parameter(name = "shopId", description = "店铺id")
    @PreAuthorize("@pms.hasPermission('platform:shopBankCard:list')")
    public ServerResponseEntity<List<ShopBankCard>> getShopBankCardList(@RequestParam(value = "shopId") Long shopId) {
        List<ShopBankCard> list = shopBankCardService.list(new LambdaQueryWrapper<ShopBankCard>().eq(ShopBankCard::getShopId, shopId).eq(ShopBankCard::getStatus, 1));
        return ServerResponseEntity.success(list);
    }

    @PostMapping("/allinpaySaveAndApplyShop")
    @Operation(summary = "通联支付--批量保存店铺银行卡信息并提交店铺审核信息", description = "批量保存店铺银行卡信息并提交店铺审核信息")
    @PreAuthorize("@pms.hasPermission('platform:shopBankCard:save')")
    public ServerResponseEntity<Void> allinpaySaveAndApplyShop(@Valid @RequestBody AllinpayShopBankCardDTO allinpayShopBankCardDTO) {
        Long shopId = allinpayShopBankCardDTO.getShopId();
        if (Objects.isNull(shopId)) {
            throw new YamiShopBindException("店铺id不能为空");
        }
        // 查找工商信息
        ShopCompanyVO shopCompany = shopCompanyService.getShopCompanyByShopIdAndStatus(shopId, AuditStatus.WAITAUDIT.value());
        if (Objects.isNull(shopCompany)) {
            throw new YamiShopBindException("该店铺工商信息为空，请刷新页面重新填写");
        }
        checkAllinpay(shopCompany);
        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
        if (Objects.equals(shopDetail.getCompanyInfoProcessStatus(), CompanyInfoProcessStatus.SUCCESS.value())) {
            // 已经审核成功就不必要多次设置企业信息了
            throw new YamiShopBindException("该店铺已经审核通过");
        }

        shopBankCardService.insertAndSetCompanyInfo(allinpayShopBankCardDTO, shopId, BeanUtil.map(shopCompany, ShopCompany.class));
        shopDetailService.removeShopDetailCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }

    private void checkAllinpay(ShopCompanyVO shopCompany) {
        boolean needInfo = Objects.isNull(shopCompany.getLegalIds()) || Objects.isNull(shopCompany.getLegalPhone());
        if (allinpayCompanyService.getIsAllinpay() && needInfo) {
            throw new YamiShopBindException("工商信息需要提交的内容有所变更，请重新填写");
        }
    }

}
